package com.sunzn.media.library.frgm;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ViewAnimator;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sunzn.media.library.R;
import com.sunzn.media.library.adpt.MediaAdapter;
import com.sunzn.media.library.bean.Album;
import com.sunzn.media.library.bean.Item;
import com.sunzn.media.library.help.MediaGridInset;
import com.sunzn.media.library.help.SelectedItemCollection;
import com.sunzn.media.library.load.AlbumMediaCollection;
import com.sunzn.media.library.port.AlbumViewModel;
import com.sunzn.media.library.port.MediaViewModel;

import static com.sunzn.media.library.load.AlbumMediaLoader.WHOLE;

public class MediaFragment extends Fragment implements AlbumMediaCollection.AlbumMediaCallbacks, MediaAdapter.CheckStateListener, MediaAdapter.OnMediaClickListener {

    public static final String EXTRA_ALBUM = "extra_album";
    public static final String EXTRA_SORT = "extra_sort";
    private RecyclerView mRecyclerView;

    private SelectionProvider mSelectionProvider;
    private MediaAdapter.CheckStateListener mCheckStateListener;
    private MediaAdapter.OnMediaClickListener mOnMediaClickListener;

    private final AlbumMediaCollection mAlbumMediaCollection = new AlbumMediaCollection();
    private MediaAdapter adapter;
    private Album album;
    private int sort;
    private ViewAnimator mViewAnimator;

    public interface SelectionProvider {
        SelectedItemCollection provideSelectedItemCollection();
    }

    public static MediaFragment instance(Album album, int sort) {
        Log.e("TTT", "MediaFragment Album = " + album);
        MediaFragment fragment = new MediaFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(EXTRA_SORT, sort);
        bundle.putParcelable(EXTRA_ALBUM, album);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getActivity() != null) {
            MediaViewModel actionViewModel = new ViewModelProvider(getActivity()).get(MediaViewModel.class);
            actionViewModel.getAction().observe(this, new Observer<Long>() {
                @Override
                public void onChanged(Long aLong) {
                    adapter.notifyDataSetChanged();
                }
            });
            AlbumViewModel albumViewModel = new ViewModelProvider(getActivity()).get(AlbumViewModel.class);
            albumViewModel.getAlbum().observe(this, new Observer<Album>() {
                @Override
                public void onChanged(Album album) {
                    mAlbumMediaCollection.onDestroy();
                    mAlbumMediaCollection.onCreate(getActivity(), MediaFragment.this);
                    mAlbumMediaCollection.load(album, sort, false);
                }
            });
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mAlbumMediaCollection.onDestroy();
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof SelectionProvider) {
            mSelectionProvider = (SelectionProvider) context;
        } else {
            throw new IllegalStateException("Context must implement SelectionProvider.");
        }
        if (context instanceof MediaAdapter.CheckStateListener) {
            mCheckStateListener = (MediaAdapter.CheckStateListener) context;
        }
        if (context instanceof MediaAdapter.OnMediaClickListener) {
            mOnMediaClickListener = (MediaAdapter.OnMediaClickListener) context;
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_media_container, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView = view.findViewById(R.id.recyclerview);
        mViewAnimator = view.findViewById(R.id.animator);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getArguments() != null) {
            sort = getArguments().getInt(EXTRA_SORT, WHOLE);
            album = getArguments().getParcelable(EXTRA_ALBUM);
        }

        adapter = new MediaAdapter(mSelectionProvider.provideSelectedItemCollection(), mRecyclerView);
        adapter.registerCheckStateListener(this);
        adapter.registerOnMediaClickListener(this);

        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
        mRecyclerView.addItemDecoration(new MediaGridInset(4, 3, false));
        mRecyclerView.setAdapter(adapter);

        mAlbumMediaCollection.onCreate(getActivity(), this);
        mAlbumMediaCollection.load(album, sort, false);
    }

    @Override
    public void onAlbumMediaLoad(Cursor cursor) {
        mViewAnimator.setDisplayedChild(cursor == null || cursor.getCount() == 0 ? 1 : 0);
        adapter.swapCursor(cursor);
    }

    @Override
    public void onAlbumMediaReset() {
        adapter.swapCursor(null);
    }

    @Override
    public void onUpdate() {
        if (mCheckStateListener != null) {
            mCheckStateListener.onUpdate();
        }
    }

    @Override
    public void onMediaClick(Album album, Item item, int adapterPosition) {
        if (mOnMediaClickListener != null && getArguments() != null) {
            mOnMediaClickListener.onMediaClick(getArguments().getParcelable(EXTRA_ALBUM), item, adapterPosition);
        }
    }

}
